package com.api.syh56.shengyunhui.controller.admin;

import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.entity.Agreement;
import com.api.syh56.shengyunhui.entity.dto.AgreementDto;
import com.api.syh56.shengyunhui.entity.dto.AgreementKDTO;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.service.AdminAgreementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 协议管理
 */
@RestController
@RequestMapping("admin/agreement")
public class AdminAgreementController {
    @Autowired
    private AdminAgreementService adminAgreementService;

//    /**
//     * 添加协议
//     * @param agreement
//     * @return
//     */
//    @PostMapping("insert")
//    public CommonResult<Object> insert(@RequestBody Agreement agreement){
//        adminAgreementService.insert(agreement);
//        return CommonResult.success(null);
//    }

    /**
     * 查看协议
     * @param
     * @return
     */
    @GetMapping("select")
    public CommonResult<List<Agreement>> select(){
        List<Agreement> select = adminAgreementService.select();
        return CommonResult.success(select);
    }

    /**
     * 查看协议（关键词查询）
     * @param keyword
     * @return
     */
    @GetMapping("selectBykeyword")
    public CommonResult<Agreement> selectBy(@RequestParam String keyword){
        Agreement agreement = adminAgreementService.selectByKeyword(keyword);
        return CommonResult.success(agreement);
    }

    /**
     * 协议展示
     * @param keyword
     * @return
     */
    @GetMapping("selectBykeywordhtml")
    public String selectBykeywordhtml(@RequestParam String keyword){
        Agreement agreement = adminAgreementService.selectByKeyword(keyword);
        return agreement.getText();
    }


    /**
     * 通过id查看协议
     * @param id
     * @return
     */
    @GetMapping("selectbyid")
    public CommonResult<Object> selectBy(@RequestParam Integer id){
        Object select = adminAgreementService.selectById(id);
        return CommonResult.success(select);
    }

    /**
     * 修改协议
     * @param map
     * @return
     */
    @PostMapping("update")
    public CommonResult<Object> update(@RequestBody Map<String,String> map){
        System.out.println(map);
        System.out.println(map.keySet());
        adminAgreementService.updateBy(map);
        return CommonResult.success(null);
    }

//    @PostMapping("updateBy")
//    public CommonResult<Object> updateBy(@RequestBody AgreementKDTO agreementKDTO){
//        adminAgreementService.updateByKey(agreementKDTO);
//        return CommonResult.success(null);
//    }
    /**
     * 删除协议
     * @param id
     * @return
     */
    @PostMapping("delete")
    public CommonResult<Object> delete(@RequestParam Integer id){
        if (id == null) {
            throw new BaseException("id不为null");
        }
        adminAgreementService.delete(id);
        return CommonResult.success(null);
    }
}
